package team.shixi.houtai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.shixi.houtai.common.Result;
import team.shixi.houtai.common.ResultUtil;
import team.shixi.houtai.entity.Category;
import team.shixi.houtai.service.CategoryService;


/**
 * @author Zhangrt
 * @Description
 * @create 2023-01-10 17:00
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping//新增数据
    public Result<?> save(@RequestBody Category category){
        categoryService.save(category);
        return Result.success();
    }

    @PutMapping//更新数据
    public Result<?> update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.success();
    }


    @DeleteMapping("/{id}")//删除数据
    public Result<?> delete(@PathVariable Long id){
        categoryService.removeById(id);
        return Result.success();
    }

    @GetMapping//查询数据
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        Page<Category> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like((StringUtils.isNotBlank(search)),Category::getType,search);
        Page<Category> categoryPage = categoryService.page(page, queryWrapper);
        return ResultUtil.success(categoryPage);
    }
}

